<?php

class Login extends CI_Controller{
private $data;
	public function __construct(){
		parent::__construct();
		$this->data['url1']=site_url(array("login","login","validar"));
		$this->data['url2']=site_url(array("inicio","inicio"));
	}
	
	public function index(){
		session_start();
		if(isset($_SESSION['VALID']) && $_SESSION['VALID'] == 1){
			redirect('inicio/inicio');
		}else{
			$this->load->view("cabecera_login");
			$this->load->view("login/v_entrar",$this->data);
			$this->load->view("footer");
		}
	}
	

	public function validar(){
		if(isset($_SESSION['VALID']) && $_SESSION['VALID'] == 1){
				redirect('inicio/inicio');
			}else{
				$usuario=$this->input->post("user");
				$clave=$this->input->post("pass");
				$this->load->model("login/login_model");
				$ret=$this->login_model->login($usuario,$clave);
				if($ret!=0){
					session_start();
					$_SESSION['VALID']=1;
					foreach($ret as $k=>$v){
						if($k!="ACCESOS"){
							echo $_SESSION[$k]=$v;
						}else{
							 $_SESSION['ACCESOS']=$this->accesos($ret['ACCESOS']);
						}
					}
					echo 1;
				}else{
					echo 0;
				}
			}
	}
	
	public function logout(){
		session_start();
		session_destroy();
		redirect('login/login');
	}
	
	private function accesos($acceso){
		$menu=$this->config->item('menu');
		$m=explode(',',$acceso);
		$m1=array();
		foreach($m as $k=>$v){
			if($v==1){
				$m1[]=$menu[$k];
			}
		}
		return $m1;
	}
	
	

}